package UI.MultiPlayer;

import java.awt.EventQueue;
import java.io.File;
import javax.swing.*;

import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import UI.MultiPlayer.PlayWindow;

/**
 * @author 杨明政
 * @version  2021.0520
 * @since 1.8
 */


/**
 * 视频播放器类.
 * For example:
 * <pre>
 * VideoPlayer u=new VideoPlayer();
 * u.generate(panel,url)
 * </pre>
 * @author 杨明政
 */

public class VideoPlayer {
    public static PlayWindow frame;
    /**
     * Returns the character at the specified index. An index
     * ranges from <code>0</code> to <code>length() - 1</code>.
     *
     * @param  panel  视频资源位置
     * @param url 视频地址
     */
    public static void generate(JPanel panel,String url) {
        new NativeDiscovery().discover();
        if (RuntimeUtil.isWindows()) {
            NativeLibrary.addSearchPath(
                    RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
        }else if (RuntimeUtil.isMac()) {
            NativeLibrary.addSearchPath(
                    RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib");
        }else if (RuntimeUtil.isNix()) {
            NativeLibrary.addSearchPath(
                    RuntimeUtil.getLibVlcLibraryName(), "/home/linux/vlc/install/lib");
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new PlayWindow();
                    frame.setVisible(true);
                    //让窗体获得视频资源
                    frame.getMediaPlayer().playMedia(url);
                    new SwingWorker<String, Integer>() {
                        //调节视频音量
                        protected String doInBackground() throws Exception {
                            while (true) {
                                //获得当前视频总时间长度
                                long total = frame.getMediaPlayer().getLength();
                                //获得当期播放时间
                                long curr = frame.getMediaPlayer().getTime();
                                //获取播放视频的百分比
                                if(total>6000)total=6000;
                                if(curr>total)
                                    frame.getMediaPlayer().stop();
                            }
                        }
                    }.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
